<?php
require_once 'init.php';

// remove a row
if ($_GET['act'] == 'remove'){
	$db = $_GET['db'];
	$collection = $_GET['collection'];
	$id = $_GET['id'];
	$phpmongo = new Phpmongo($db, $collection);	
	$phpmongo->remove(array('_id' => new MongoId($id)));
	message("document deleted.", "collection.php?db=$db&collection=$collection");
}

// update
if ($_POST['act'] == 'update'){
	$db = $_POST['db'];
	$collection = $_POST['collection'];
	$id = $_POST['id'];	
	$newobj = $_POST['newobj'];
	$phpmongo = new Phpmongo($db, $collection);	
	$phpmongo->update(array('_id' => new MongoId($id)),$newobj);
	message("document updated.", "collection.php?db=$db&collection=$collection");	
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="css/style.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="js/common.js"></script>
<title>PhpMongo</title>
</head>
<body>
<div class="warp_left">
<?php
require_once 'left.php';
?>	
</div>
<div class="warp_right">

<?php
$db = $_GET['db'];
$collection = $_GET['collection'];
$id = $_GET['id'];
$phpmongo = new Phpmongo($db, $collection);
$row = $phpmongo->findById($id);
?>
<p>
<b>&nbsp;&nbsp;&nbsp;&nbsp;db:</b> <?php echo $db;?>
<b>&nbsp;&nbsp;&nbsp;&nbsp;dcollection:</b> <?php echo $collection;?>
<b>&nbsp;&nbsp;&nbsp;&nbsp;_id:</b> <?php echo $id;?>
</p>
	<div>
		<form action="document.php" method="POST">
				<table>
					<tr>
						<td>Update *:</td>
						<td>
						<textarea name="newobj" rows="7" cols="80"><?php unset($row['_id']);var_export($row);?></textarea>
						</td>
					</tr>
					<tr>
						<td>
						<input type="hidden" name="db" value="<?php echo $db;?>" />
						<input type="hidden" name="collection" value="<?php echo $collection;?>" />
						<input type="hidden" name="id" value="<?php echo $id;?>" />
						<input type="hidden" name="act" value="update" />
						<input type="submit" value="Query"  class="submit" />
						
						</td>
						<td>
						<a href="<?php echo 'collection.php?db='.$db.'&collection='.$collection;?>">Back to collection</a>
						</td>
					</tr>
			
		</form>
	</div>
</div>

</body>
</html>